package com.thermofisher.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thermofisher.beans.Employee;
import com.thermofisher.beans.Leads;
import com.thermofisher.service.EmployeeService;
import com.thermofisher.service.LeadsService;
import com.thermofisher.utils.MyHttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@EnableScheduling
public class GrowthClick
{

    private Logger log = LoggerFactory.getLogger(GrowthClick.class);

    @Autowired
    private MyHttpClientUtils myHttpClientUtils;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private LeadsService leadsService;

    @Scheduled(cron = "0 0 8 * * ?")
    public void growthClickSchedule()
    {
        log.info("=====GrowthClick数据同步开始=========");
        boolean result = saveEmployeeData();
        log.info("=====GrowthClick数据同步结束======result===" + result);
    }

    //@Scheduled(cron = "0 */1 * * * ?")
    public void leadSchedule()
    {
        log.info("=====GrowthClick数据同步开始=========");
        boolean result = saveLeadsData();
        log.info("=====GrowthClick数据同步结束======result===" + result);
    }

    public Boolean saveEmployeeData()
    {
        List<Employee> list = getGrowthClickData();
        log.info("======list=======" + list.size());
        boolean result = employeeService.saveOrUpdateBatch(list);
        log.info("======result=======" + result);
        return result;
    }

    public Boolean saveLeadsData()
    {
        List<Leads> list = getLeadDataList();
        log.info("======list=======" + list.size());
        boolean result = leadsService.saveOrUpdateBatch(list);
        log.info("======result=======" + result);
        return result;
    }

    public List<Leads> getLeadDataList()
    {
        List<Leads> list = new ArrayList<>();
        String url = "https://thermo.peoplus.cn/pull_api/tmo_leads";
        String jsonstr = myHttpClientUtils.requestPost(url);
        JSONObject rootObject = JSON.parseObject(jsonstr);
        JSONArray jsonArray = rootObject.getJSONObject("result").getJSONObject("data")
                .getJSONArray("query_data");
        for (int i = 1; i < jsonArray.size(); i++)
        {
            Object object = jsonArray.get(i);
            String jsonArray2 = JSON.toJSONString(object);
            JSONArray jsonArray3 = JSONArray.parseArray(jsonArray2);
            Leads lead = getLeadData(jsonArray3);
            list.add(lead);
        }
        return list;
    }

    public Leads getLeadData(JSONArray jsonArray3)
    {
        Leads Lead = new Leads();
        Lead.setLeadNo(String.valueOf(jsonArray3.get(0)));
        Lead.setSubmitDate(String.valueOf(jsonArray3.get(1)));
        Lead.setProviderName(String.valueOf(jsonArray3.get(2)));
        Lead.setSubmitBu(String.valueOf(jsonArray3.get(3)));
        Lead.setWorkPhone(String.valueOf(jsonArray3.get(4)));
        Lead.setWorkEmail(String.valueOf(jsonArray3.get(5)));
        Lead.setStatus(String.valueOf(jsonArray3.get(6)));
        Lead.setSfdcId(String.valueOf(jsonArray3.get(7)));
        Lead.setLockStatus(String.valueOf(jsonArray3.get(8)));
        Lead.setCompanyName(String.valueOf(jsonArray3.get(9)));
        Lead.setContactName(String.valueOf(jsonArray3.get(10)));
        Lead.setPurchasePlan(String.valueOf(jsonArray3.get(11)));
        Lead.setContactPhone(String.valueOf(jsonArray3.get(12)));
        Lead.setInterestedProducts(String.valueOf(jsonArray3.get(13)));
        Lead.setDescription(String.valueOf(jsonArray3.get(14)));
        Lead.setLeaderName(String.valueOf(jsonArray3.get(15)));
        Lead.setFollowUpBu(String.valueOf(jsonArray3.get(16)));
        Lead.setLeaderPhone(String.valueOf(jsonArray3.get(17)));
        Lead.setLeaderEmail(String.valueOf(jsonArray3.get(18)));
        Lead.setAcceptDate(String.valueOf(jsonArray3.get(19)));
        Lead.setVerifyDate(String.valueOf(jsonArray3.get(20)));
        Lead.setEstimatAmount(String.valueOf(jsonArray3.get(21)));
        Lead.setRevenue(String.valueOf(jsonArray3.get(22)));
        Lead.setVerifyStatus(String.valueOf(jsonArray3.get(23)));
        Lead.setOrderNumber(String.valueOf(jsonArray3.get(24)));
        Lead.setRightOrderNumber(String.valueOf(jsonArray3.get(25)));
        Lead.setDeadlineTime(String.valueOf(jsonArray3.get(26)));
        Lead.setVerificationRemarks(String.valueOf(jsonArray3.get(27)));
        Lead.setRemarks(String.valueOf(jsonArray3.get(28)));
        return Lead;
    }

    public List<Employee> getGrowthClickData()
    {
        List<Employee> list = new ArrayList<>();
        String url = "https://thermo.peoplus.cn/pull_api/hr_employee";
        String jsonstr = myHttpClientUtils.requestPost(url);
        JSONObject rootObject = JSON.parseObject(jsonstr);
        JSONArray jsonArray = rootObject.getJSONObject("result").getJSONObject("data")
                .getJSONArray("query_data");
        for (int i = 1; i < jsonArray.size(); i++)
        {
            Object object = jsonArray.get(i);
            String jsonArray2 = JSON.toJSONString(object);
            JSONArray jsonArray3 = JSONArray.parseArray(jsonArray2);
            Employee employee = getEmployeeData(jsonArray3);
            list.add(employee);
        }
        return list;
    }

    public Employee getEmployeeData(JSONArray jsonArray3)
    {
        Employee employee = new Employee();
        employee.setEmployeeNumber(String.valueOf(jsonArray3.get(0)));
        //employee.setEmployeeName(String.valueOf(jsonArray3.get(1)));
        employee.setDepartmentCode(String.valueOf(jsonArray3.get(2)));
        employee.setDepartmentName(String.valueOf(jsonArray3.get(3)));
        employee.setJobCode(String.valueOf(jsonArray3.get(4)));
        employee.setJobName(String.valueOf(jsonArray3.get(5)));
        employee.setParentId(String.valueOf(jsonArray3.get(6)));
        employee.setCountryName(String.valueOf(jsonArray3.get(7)));
        employee.setWorkActivity(String.valueOf(jsonArray3.get(8)));
        employee.setIsClt(String.valueOf(jsonArray3.get(9)));
        employee.setNotUpdateBu(String.valueOf(jsonArray3.get(10)));
        employee.setHiredate(String.valueOf(jsonArray3.get(11)));
        employee.setJodFamilyGroup(String.valueOf(jsonArray3.get(12)));
        employee.setJodFamily(String.valueOf(jsonArray3.get(13)));
        employee.setBuCode(String.valueOf(jsonArray3.get(14)));
        employee.setBuName(String.valueOf(jsonArray3.get(15)));
        employee.setWorkEmail(String.valueOf(jsonArray3.get(16)));
        employee.setWorkPhone(String.valueOf(jsonArray3.get(17)));
        employee.setMobilePhone(String.valueOf(jsonArray3.get(18)));
        return employee;
    }

}
